
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;



public class RailRoadUI extends JFrame implements Observer {

	private JButton buyBtn, rentBtn, returnBtn, atmBtn, bidBtn;
	private JPanel contentPane;
	private JLabel ownerNameLbl, costLbl, costLabel, currentMoneyLbl, currentMoneyLabel;
	private RailRoad currentRailRoad;
	private Player p;
	private Player[] playArray;
	private RailRoad[] rr;
	private Block[] bl;
	private int tempCurrentPlayerMoney;
	private ImageIcon propertyImage;
	private Sound sound;

	public RailRoadUI(Player play, RailRoad[] railRoad) {
		this.p=play;
		this.rr=railRoad;
		this.tempCurrentPlayerMoney = this.p.getCurrentMoney();
		System.out.println(this.rr.length+" supposed to be 4");
	
		setCurrentRailRoad();
		
		this.propertyImage=new ImageIcon(this.currentRailRoad.getPropertyImage());
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 560, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		this.setVisible(true);
		this.setSize(544,350);
		
		JLabel label = new JLabel(propertyImage);
		label.setBackground(Color.WHITE);
		label.setBounds(10, 11, 254, 300);
		contentPane.add(label);
		
		ImageIcon buyImg = new ImageIcon("Graphics/buy.png");
		ImageIcon payImg = new ImageIcon("Graphics/pay.png");
		ImageIcon rtnImg = new ImageIcon("Graphics/rtn.png");
		ImageIcon bidImg = new ImageIcon("Graphics/bid.png");
		ImageIcon atmImg = new ImageIcon("Graphics/atm.png");
		
		buyBtn = new JButton(buyImg);
		buyBtn.setBounds(285, 265, 50, 50);
		buyBtn.setVisible(false);
		buyBtn.setBackground(null);
		buyBtn.addMouseListener(null);
		buyBtn.setBorderPainted(false);
		buyBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		buyBtn.setContentAreaFilled(false);
        buyBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buySelect(evt);
            }
        });
		contentPane.add(buyBtn);
		
		bidBtn = new JButton(bidImg);
		bidBtn.setBounds(365, 265, 50, 50);
		bidBtn.setVisible(false);
		bidBtn.setBackground(null);
		bidBtn.addMouseListener(null);
		bidBtn.setBorderPainted(false);
		bidBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		bidBtn.setContentAreaFilled(false);
        bidBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bidSelect(evt);
            }
        });
		contentPane.add(bidBtn);
		
		atmBtn = new JButton(atmImg);
		atmBtn.setBounds(445, 265, 50, 50);
		atmBtn.setVisible(false);
		atmBtn.setBackground(null);
		atmBtn.addMouseListener(null);
		atmBtn.setBorderPainted(false);
		atmBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		atmBtn.setContentAreaFilled(false);
        atmBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atmSelect(evt);
            }
        });
		contentPane.add(atmBtn);
		
		
		rentBtn = new JButton(payImg);
		rentBtn.setBounds(285, 265, 50, 50);
		rentBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		rentBtn.setVisible(false);
		rentBtn.setBackground(null);
		rentBtn.setBorderPainted(false);
		rentBtn.setContentAreaFilled(false);
        rentBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rentSelect(evt);
            }
        });
		contentPane.add(rentBtn);
		
		returnBtn = new JButton(rtnImg);
		returnBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
		returnBtn.setBounds(285, 265, 50, 50);
		returnBtn.setVisible(false);
		returnBtn.setBackground(null);
		returnBtn.setBorderPainted(false);
		returnBtn.setContentAreaFilled(false);
        returnBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnSelect(evt);
            }
        });
		contentPane.add(returnBtn);
		
		costLbl = new JLabel("Cost:");
		costLbl.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		costLbl.setBounds(274, 250, 33, 14);
		costLbl.setVisible(false);
		contentPane.add(costLbl);
		
		costLabel = new JLabel(Integer.toString(this.currentRailRoad.getPrice()));
		costLabel.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		costLabel.setBounds(321, 250, 33, 14);
		costLabel.setVisible(false);
		contentPane.add(costLabel);
		
		checkOwner();
		checkRentPrice();

		JLabel lblPropName = new JLabel("RailRoad Name:");
		lblPropName.setFont(new Font("Viner Hand ITC", Font.BOLD | Font.ITALIC, 14));
		lblPropName.setBounds(274, 0, 131, 32);
		contentPane.add(lblPropName);
		
		JLabel propName = new JLabel(this.currentRailRoad.getName());
		propName.setFont(new Font("Tahoma", Font.BOLD, 14));
		propName.setBounds(400, 0, 182, 32);
		contentPane.add(propName);
		
		JLabel lblWithHouse = new JLabel("If 1 RR is owned:");
		lblWithHouse.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		lblWithHouse.setBounds(290, 78, 115, 32);
		contentPane.add(lblWithHouse);
		
		JLabel withHouse = new JLabel(Integer.toString(this.currentRailRoad.getRent0()));
		withHouse.setFont(new Font("Tahoma", Font.BOLD, 12));
		withHouse.setBounds(400, 78, 182, 32);
		contentPane.add(withHouse);
		
		JLabel lblWithHouses = new JLabel("If 2 RR's are owned:");
		lblWithHouses.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		lblWithHouses.setBounds(274, 96, 131, 32);
		contentPane.add(lblWithHouses);
		
		JLabel withHouses = new JLabel(Integer.toString(this.currentRailRoad.getRent1()));
		withHouses.setFont(new Font("Tahoma", Font.BOLD, 12));
		withHouses.setBounds(400, 96, 182, 32);
		contentPane.add(withHouses);
		
		JLabel lblWithHouses_1 = new JLabel("If 3 RR's are owned:");
		lblWithHouses_1.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		lblWithHouses_1.setBounds(274, 115, 131, 32);
		contentPane.add(lblWithHouses_1);
		
		JLabel withHouses_1 = new JLabel(Integer.toString(this.currentRailRoad.getRent2()));
		withHouses_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		withHouses_1.setBounds(400, 115, 182, 32);
		contentPane.add(withHouses_1);
		
		JLabel lblWithHouses_2 = new JLabel("If 4 RR's are owned:");
		lblWithHouses_2.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		lblWithHouses_2.setBounds(274, 133, 131, 32);
		contentPane.add(lblWithHouses_2);
		
		JLabel withHouses_2 = new JLabel(Integer.toString(this.currentRailRoad.getRent3()));
		withHouses_2 .setFont(new Font("Tahoma", Font.BOLD, 12));
		withHouses_2 .setBounds(400, 133, 182, 32);
		contentPane.add(withHouses_2 );
		
		JLabel lblMortgageValue = new JLabel("Mortgage Value:");
		lblMortgageValue.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		lblMortgageValue.setBounds(274, 172, 131, 32);
		contentPane.add(lblMortgageValue);
		
		JLabel mortgageValue = new JLabel(Integer.toString(this.currentRailRoad.getMortgageValue()));
		mortgageValue.setFont(new Font("Tahoma", Font.BOLD, 12));
		mortgageValue.setBounds(400, 172, 182, 32);
		contentPane.add(mortgageValue);
		
		JLabel label_1 = new JLabel("Current Rent:");
		label_1.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		label_1.setBounds(307, 54, 84, 32);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel(Integer.toString(this.currentRailRoad.getCurrentRent()));
		label_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_2.setBounds(400, 53, 182, 32);
		contentPane.add(label_2);
		
		JLabel ownerLbl = new JLabel("Current Owner:");
		ownerLbl.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		ownerLbl.setBounds(298, 40, 80, 15);
		contentPane.add(ownerLbl);
		
		if(this.currentRailRoad.getCOwner()==null){
			ownerNameLbl = new JLabel("The Bank");
		}else{
			ownerNameLbl = new JLabel(this.currentRailRoad.getCOwner().getPlayerName());
		}
		ownerNameLbl.setFont(new Font("Tahoma", Font.BOLD, 12));
		ownerNameLbl.setBounds(400, 35, 182, 23);
		contentPane.add(ownerNameLbl);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(268, 5, 254, 230);
		contentPane.add(panel);
		
		currentMoneyLbl = new JLabel("Your Current Money:");
		currentMoneyLbl.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		currentMoneyLbl.setBounds(364, 250, 115, 14);
		currentMoneyLbl.setVisible(true);
		contentPane.add(currentMoneyLbl);

		currentMoneyLabel = new JLabel(Integer.toString(this.tempCurrentPlayerMoney));
		currentMoneyLabel.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
		currentMoneyLabel.setBounds(485, 250, 80, 14);
		currentMoneyLabel.setVisible(true);
		contentPane.add(currentMoneyLabel);
		
		this.sound = new Sound();
		this.sound.playLoop("Sounds/train.wav");
		
		
	}
	
   private void buySelect(java.awt.event.ActionEvent evt) {
	   if(this.p.getCurrentMoney()>=this.currentRailRoad.getPrice()){
		   this.p.subtractMoney(this.currentRailRoad.getPrice());
	       this.currentRailRoad.setCOwner(this.p);
	       new Sound().play("Sounds/buyBlock.wav");
	       
	       Command cmd = new NextPlayerCommand();
	       cmd.execute();
	       
	       this.dispose();
	       this.sound.getClip().stop();
       }else{
    	   JOptionPane.showMessageDialog(null, "You don't have enough money to buy this. Click on the ATM button to mortgage property or sell homes if you can.");
       } 
   }
   
   private void rentSelect(java.awt.event.ActionEvent evt) {
	   int tempCurrentPlayerMoney = 0;
	   
	   if(this.p.getCurrentMoney()>=this.currentRailRoad.getCurrentRent()){
	       this.p.subtractMoney(this.currentRailRoad.getCurrentRent()*this.p.getLastRoll());      
	       this.currentRailRoad.getCOwner().addMoney(this.currentRailRoad.getCurrentRent()*this.p.getLastRoll());
		   
	       Command cmd = new NextPlayerCommand();
	       cmd.execute();
       
	       this.dispose();
	       this.sound.getClip().stop();
	   }else{
		   CommandWithReturn cmdWithReturn = new BankruptcyCheckerCommand();
		   tempCurrentPlayerMoney=cmdWithReturn.execute();
		   
		   if(tempCurrentPlayerMoney<=this.currentRailRoad.getCurrentRent()){
			   JOptionPane.showMessageDialog(null, "Kawawa. Bankrupt ka na! Laro ka na lang ng checkers. Wag kang mag alala, kasama naman si Nette.");
			   Command cmd = new TransferAssetsCommand();
			   cmd.execute();
			   this.p.setActive(false);
			   this.dispose();
			   this.sound.getClip().stop();
		   }else{
			   JOptionPane.showMessageDialog(null, "Hoy, conti nalang ang pera mo. Benta ka na bahay o imortgage mo ang properties mo."); 
		   }
	   }
   }
   
   private void returnSelect(java.awt.event.ActionEvent evt) {
       Command cmd= new NextPlayerCommand();
       cmd.execute();
       this.dispose();
       this.sound.getClip().stop();
   }
   
   private void bidSelect(java.awt.event.ActionEvent evt) {
	   CommandWithInt cmdInt = new PropertySelectionUICommand();
       cmdInt.execute(6);

       this.dispose();
       this.sound.getClip().stop();
   }
   
   private void atmSelect(java.awt.event.ActionEvent evt) {
	   CommandWithInt cmdInt = new PropertySelectionUICommand();
	   cmdInt.execute(2);
   }
   
   public void setCurrentRailRoad(){
	   for(int i=0; i<this.rr.length;i++){
		   if(this.p.getCurrentBlockPos()==rr[i]){
			   this.currentRailRoad=rr[i];
		   }
	   }
   }
   
   public void checkOwner(){
		if(this.currentRailRoad.getCOwner()==null){
			buyBtn.setVisible(true);
			bidBtn.setVisible(true);
			atmBtn.setVisible(true);
			this.costLbl.setVisible(true);
			this.costLabel.setVisible(true);
		}else if(this.currentRailRoad.getCOwner()!=this.p){
			if(this.currentRailRoad.getIsMortgaged()==true){
				JOptionPane.showMessageDialog(null, "Libre ang rent ngyon kasi mortgaged ang intong property!");
				returnBtn.setVisible(true);
			}else{
			rentBtn.setVisible(true);
			}
		}else{
			returnBtn.setVisible(true);
		}	
   }
   
   public void checkRentPrice(){
	   int cRent=0;
	   for(int i=0;i<rr.length;i++){
		   if(this.currentRailRoad.getCOwner()==this.rr[i].getCOwner() && currentRailRoad!=rr[i] && this.currentRailRoad.getCOwner()!=null){
			   cRent++;
		   }
	   }
	   this.currentRailRoad.setCurrentRent(cRent);
   }
   
   public void update(Observable obj, Object arg){
	   tempCurrentPlayerMoney=(Integer) arg;

	   this.currentMoneyLabel.setVisible(false);
	   contentPane.remove(this.currentMoneyLabel);
	   this.currentMoneyLabel = new JLabel(Integer.toString(this.tempCurrentPlayerMoney));
	   currentMoneyLabel.setFont(new Font("Microsoft Tai Le", Font.BOLD | Font.ITALIC, 11));
	   currentMoneyLabel.setBounds(485, 250, 80, 14);
	   currentMoneyLabel.setVisible(true);
	   contentPane.add(this.currentMoneyLabel);
   }
}
